//
//  MYPortraitRadioGroup.m
//  HeXun-zixuan
//
//  Created by 范名扬 on 16/2/24.
//  Copyright © 2016年 com.hztc. All rights reserved.
//

#import "MYPortraitRadioGroup.h"

@interface MYPortraitRadioGroup ()
@property (nonatomic, strong)UIImageView         *pBottomImageView;
@property (nonatomic, strong)NSMutableArray      *pButtonArray;
@property (nonatomic, strong)NSMutableArray      *pLineArray;
@end

@implementation MYPortraitRadioGroup

- (UIImageView *)pBottomImageView
{
    if (!_pBottomImageView) {
        _pBottomImageView = [[UIImageView alloc] init];
        _pBottomImageView.userInteractionEnabled = YES;
    }
    return _pBottomImageView;
}
- (NSMutableArray *)pButtonArray
{
    if (!_pButtonArray) {
        _pButtonArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pButtonArray;
}
- (NSMutableArray *)pLineArray
{
    if (!_pLineArray) {
        _pLineArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pLineArray;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setOriginalViews];
    }
    return self;
}

- (void)setOriginalViews
{
    [self addSubview:self.pBottomImageView];
    self.backgroundColor = [UIColor whiteColor];
}

- (void)setRadioGroup:(NSArray *)radioGroup
{
    [self.pButtonArray removeAllObjects];
    [self.pLineArray removeAllObjects];
    
    for (int i = 0; i < radioGroup.count; i++) {
        NSString *pTitle = radioGroup[i];
        UIButton *btn = nil;
        UIView *lineView = nil;
        btn = [[UIButton alloc] init];
        if (i < radioGroup.count - 1) {
            lineView = [[UIView alloc] init];
            lineView.backgroundColor = kMYHexToRGB(0xdddddd, 1);
        }
        btn.tag = i;
        btn.userInteractionEnabled = YES;
        [btn setTitle:pTitle forState:UIControlStateNormal];
        [btn setBackgroundColor:kMYHexToRGB(0xefefef, 1)];
        [btn setTitleColor:kMYCustomColor(85, 85, 85, 1) forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.pBottomImageView addSubview:btn];
        [self.pBottomImageView addSubview:lineView];
        if (btn) {
            [_pButtonArray addObject:btn];
        }
        if (lineView) {
            [_pLineArray addObject:lineView];
        }
    }
}

- (void)btnOnClick:(UIButton *)btn
{
    [self onClickSelectedBtn:btn];
    
    [self.parent upToParentWithObjc:self withType:kHZ_PortraitRadioGroupClick_Action withParamter:btn];
}

- (void)onClickSelectedBtn:(UIButton *)btn
{
    for (UIButton *btn in _pButtonArray) {
        btn.selected = NO;
        [btn setBackgroundColor:kMYHexToRGB(0xefefef, 1)];
    }
    btn.selected = YES;
    if (btn.selected) {
        [btn setBackgroundColor:[UIColor grayColor]];
    }
}

- (void)resetAllButtonFont:(UIFont *)font
{
    for (UIButton *btn in _pButtonArray) {
        btn.titleLabel.font = font;
    }
}

- (void)setSelectedButton:(NSInteger)index
{
    NSInteger nCount = _pButtonArray.count;
    if (index > nCount - 1 || index < 0) {
        return;
    }
    UIButton *btn = _pButtonArray[index];
    [self onClickSelectedBtn:btn];
}

- (void)layoutSubviews{
    NSInteger nCount = _pButtonArray.count;
    CGFloat nRadioH = self.frame.size.height/nCount;
    
    for (int i = 0; i < nCount; i++) {
        UIButton *btn = _pButtonArray[i];
        UIView *lineView = nil;
        if (i < nCount - 1) {
            lineView = _pLineArray[i];
        }
        btn.frame = CGRectMake(0, i*nRadioH, self.frame.size.width, nRadioH);
        lineView.frame = CGRectMake(0, CGRectGetMaxY(btn.frame) - 0.5, self.frame.size.width, 0.5);
    }
    _pBottomImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}


@end
